<template>
  <div class="flow-editor-node branch">
    <div class="top-line-mask"></div>
    <div class="top-v-line"></div>
    <div class="nodes">
      <template v-for="(item, index) in nodeConfig.nodes">
        <start
          :key="item.id"
          v-if="item.type == 'start'"
          :node_index="index"
          :nodeConfig="item"
          v-bind="$attrs"
        ></start>
        <end
          v-bind="$attrs"
          :key="item.id"
          v-if="item.type == 'end'"
          :node_index="index"
          :nodeConfig="item"
        ></end>
        <approval
          :class="{ 'has-error': !nodeValidate(item, 'approval') }"
          :key="item.id"
          v-if="item.type == 'approval'"
          :node_index="index"
          :nodeConfig="item"
          v-bind="$attrs"
        ></approval>
        <route
          :key="item.id"
          v-if="item.type == 'route'"
          :node_index="index"
          :nodeConfig="item"
          v-bind="$attrs"
        ></route>
        <condition
          :class="{ 'has-error': !nodeValidate(item, 'condition') }"
          :key="item.id"
          v-if="item.type == 'condition'"
          :node_index="index"
          :nodeConfig="item"
          :branch_index="branch_index"
          v-bind="$attrs"
        ></condition>
      </template>
    </div>
    <div class="bottom-v-line"></div>
    <div class="bottom-line-mask"></div>
  </div>
</template>

<script>
// import { Start, Approval, End, Route, Condition } from '../index.js'
import Start from '../startNode'
import Approval from '../approvalNode'
import End from '../endNode'
import Route from '../routeNode'
import Condition from '../conditionNode'
import { createRoute, createApproval } from '../../common/common'
export default {
  name: 'branch',
  components: { Start, Approval, End, Route, Condition },
  props: {
    nodeConfig: {
      type: Object,
      default: () => {
        return {}
      }
    },
    isDefault: {
      type: Boolean,
      default: false
    },
    branch_index: {
      type: Number,
      default: 0
    }
  },
  methods: {
    /**
     * @description: 用于校验节点是否填写完整
     * @param {type} 节点类型
     * @param {node} 节点信息
     */
    nodeValidate(node, type) {
      let flag = true
      switch (type) {
        case 'approval':
          // 判断是否填写assignee
          if (node.nodeConfig.approvalType) {
            if (
              node.nodeConfig.approvalType == '2' &&
              !node.nodeConfig.assignee
            ) {
              flag = false
            }
            if (
              node.nodeConfig.approvalType == '3' &&
              (node.nodeConfig.sameNodeId == '' || !node.nodeConfig.sameNodeId)
            ) {
              flag = false
            }
            if (
              node.nodeConfig.approvalType == '4' &&
              node.nodeConfig.assignee.postId == ''
            ) {
              flag = false
            }
          } else {
            flag = false
          }
          // 判断逾期处理选中委托后是否设置委托人
          if (
            node.nodeConfig.dueDateParam &&
            node.nodeConfig.dueDateParam.handingType == 'assign' &&
            node.nodeConfig.dueDateParam.assignee.userName == ''
          ) {
            flag = false
          }
          break
        case 'condition':
          if (node.title != '默认') {
            if (!node.condition) {
              flag = false
            }
          }
          break
        default:
          break
      }
      node.validate = flag // 给对应节点添加校验信息
      return flag
    }
  }
}
</script>
